﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core ;
using MShop.Core.Extensions ;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Web.Components.Paging;
using MShop.Core.Service.Membership;
using MShop.Web.Models;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class ShopUsersController : SecureController
    {
        private readonly IRoleService _RoleService;
        private readonly ICommonDao<ShopUsers> _ShopUsersDao;
        private readonly ICommonDao<aspnet_Roles> _RolesDao;
        private readonly IAreaGroupDao _AreaGroupDao;
        public ShopUsersController(ICommonDao<ShopUsers> shopUserDao,ICommonDao<aspnet_Roles> rolesDao,IRoleService roleService,IAreaGroupDao areaGroupDao)
        {
            _ShopUsersDao = shopUserDao;
            _RolesDao = rolesDao;
            _RoleService = roleService;
            _AreaGroupDao = areaGroupDao;
        }


        public ActionResult Index(string key,int? page,Guid? roleid)
        {
            var q = _ShopUsersDao.GetAll();
            int pageIndex=0;
            if(page.HasValue)
                pageIndex = page.Value-1;

            if (!string.IsNullOrEmpty(key))
            {
                q = from item in q where item.TrueName.Contains(key) || item.aspnet_Users.UserName.Contains(key) select item;
            }
            //q = from item in q where !_RoleService.PowersRoleId.Contains((Guid)item.RoleId) select item;
            if (roleid.HasValue)
            {
                q = from item in q where  item.RoleId ==roleid select item;
            }
            var q2= q.OrderByDescending(x=>x.aspnet_Users.aspnet_Membership.CreateDate);
            PagedList<ShopUsers> shopusers = new PagedList<ShopUsers>(q2, pageIndex, 30);
            return View(shopusers);
        }

        public ActionResult Create()
        {
            var q = new Article();
            ViewData["cid"] = _ShopUsersDao.GetAll().ConvertListItem("Name", "Id");
            return View(q);
        }

        [HttpPost]
        public ActionResult Create(ShopUsers article)
        {
            if (ModelState.IsValid)
            {
                _ShopUsersDao.Save(article);
                _ShopUsersDao.SaveChanges();
                return RedirectToAction("index");
            }
            else
            {
               
                return View(article);
            }
        }


        public ActionResult Edit(Guid id)
        {
            var q = _ShopUsersDao.FirstOrDefault(x => x.UserId == id);
          
            if (q.AreaId.HasValue)
            {
                ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null), q.AreaId.Value, q.AreaGroup);
            }
            else
            {
                ViewData["area"] = new SelectedAreaGroupModel(_AreaGroupDao.Find(x => x.ParentId == null));
            }
            ViewData["role"] = _RolesDao.GetAll().ConvertListItem("RoleName", "RoleId", q.RoleId);
           // ViewData["role"] = _RolesDao.Find(x => x.Description==null).ConvertListItem("RoleName", "RoleId",q.RoleId);
            return View(q);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude="LastLoginDate")] ShopUsers user, Guid id)
        {
        
            if (ModelState.IsValid)
            {
                var q = _ShopUsersDao.FirstOrDefault(x => x.UserId == id);                
                UpdateModel<ShopUsers>(q, new string[] {"AreaId","RoleId","TrueName","Sex","Born","Tel","Mobile","Fax","QQ","Msn","IdCard","Address","Memo" });
                _RoleService.UpdateUserRole(q.aspnet_Users.UserName, _RolesDao.Get(x=>x.RoleId==user.RoleId).RoleName);
                _ShopUsersDao.SaveChanges();

                return RedirectToAction("index");
            }
            ViewData["role"] = _RolesDao.GetAll().ConvertListItem("RoleName", "RoleId", user.RoleId);
            //ViewData["role"] = _RolesDao.Find(x => x.Description == null).ConvertListItem("RoleName", "RoleId", user.RoleId);
            return View(user);
        }

    
    }
}
